package org.ruanjj.maxprofit;

public class Solution {
    public int maxProfit(int[] prices) {
        int n = prices.length;
        int min = prices[0];
        int maxProfitVal = 0;

        for(int i = 0; i < n; i++) {
            if (prices[i] < min) {
                min = Math.min(min, prices[i]);
            } else {
                maxProfitVal = Math.max(maxProfitVal,prices[i]-min);
            }
        }
        return maxProfitVal;
    }
}
